<?php

	class job_wanted extends Zcontroller {

	function __construct() {
			parent::__construct();
	}


	function index()
	{
		$Z_USER = Zsession::getUserFromSession();
		if(isset($Z_USER["user_id"])){
			$this->load->helper(array('form', 'url'));
			$this->load->template("templates/general","job_wanted/job_wanted","Zuka - Post job wanted");
		}else
		redirect(site_url("users"));
	}


	function job_wanted_list() {
		$this->load->model("m_jobwanted");
			//load pagination class
		$this->load->library('pagination');
		$config['base_url'] = site_url("/job_wanted/job_wanted_list");
		$config['total_rows'] = $this->db->count_all('job_wanted');
		$config['per_page'] = '16';
		$config['num_links'] = '16';
		$config['full_tag_open'] = '<p>';
		$config['full_tag_close'] = '</p>';
		$this->pagination->initialize($config);
			
		$data = array();
		$data["job_list"] = $this->m_jobwanted->get_job_wanted($config['per_page'],$this->uri->segment(3));
		$this->load->template("templates/general","job_wanted/job_wanted_list","Zuka - View Job Wanted", $data);
	}

	function do_upload()
	{
	 $this->load->model("m_jobwanted");
	 $this->load->library('image_lib');
	 $Z_USER = Zsession::getUserFromSession();
	 $this->load->helper('url_helper');

	 $this->load->library('form_validation');

	 $this->form_validation->set_rules('txtsub', 'job subject', 'required');
	 $this->form_validation->set_rules('txtdes', 'job description', 'required');
	 $this->form_validation->set_rules('captcha', 'security code', 'required|callback_captcha_check');
	 //$this->form_validation->set_rules('image3', 'Please upload images', '');
	 if ($this->form_validation->run() == FALSE)
	 {
	 	$data['message_errors'] = validation_errors();
		 $this->load->template("templates/general","job_wanted/job_wanted","Post Job Wanted", $data);
	 }else {
	 	/* Create the config for upload library */
	 	/* (pretty self-explanatory) */
	 	$date=date('His');
	 	$patch=FCPATH.'/usr/'.htmlspecialchars($Z_USER["user_id"]).'/'; /* NB! create this dir! */
	 	if (!file_exists ($patch)){ mkdir($patch,0777);}
	 	$config['upload_path'] = $patch;
	 	$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|docx|doc|rtf|pdf';
	 	$config['image_library'] = 'gd2';
	 	$config['source_image'] = $patch.htmlspecialchars($Z_USER["user_id"]);
	 	$config['encrypt_name']  = FALSE;
	 	$config['overwrite']  = TRUE;
	 	$config['max_size']  = '0';
	 	$config['max_width']  = '0';
	 	$config['max_height']  = '0';

	 	/* Load the upload library */
	 	$this->load->library('upload', $config);

	 	/* Create the config for image library */
	 	/* (pretty self-explanatory) */
	 	$configThumb = array();
	 	$configThumb['image_library'] = 'gd2';
	 	$configThumb['thumb_marker'] = $date.'_Thumb';
	 	$configThumb['source_image'] = '';
	 	$configThumb['create_thumb'] = TRUE;
	 	$configThumb['overwrite']  = TRUE;
	 	$configThumb['maintain_ratio'] = TRUE;
	 	/* Set the height and width or thumbs */
	 	/* Do not worry - CI is pretty smart in resizing */
	 	/* It will create the largest thumb that can fit in those dimensions */
	 	/* Thumbs will be saved in same upload dir but with a _thumb suffix */
	 	/* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
	 	$configThumb['width'] = 178;
	 	$configThumb['height'] = 210;
	 	/* Load the image library */
	 	$this->load->library('image_lib');

	 	$jsub=$_POST['txtsub'];
	 	$jdes=$_POST['txtdes'];
	 	$this->m_jobwanted->insert_job($jsub,$jdes);
	 	$jobid=$this->m_jobwanted->select_max_job_id();
	 	 
	 	$filename_arr = array();
	 	$filename_big_arr = array();
	 	for($i = 1; $i <= count($_FILES); $i++) {
	 		/* Handle the file upload */
	 		$upload = $this->upload->do_upload('image'.$i);
	 		/* File failed to upload - continue */
	 		 
	 		if($upload === FALSE) continue;
	 		/* Get the data about the file */
	 		$data = $this->upload->data();

	 		$uploadedFiles[$i] = $data;
	 		/* If the file is an image - create a thumbnail */
	 		if($data['is_image'] == 1) {
	 			$filename_arr["image".$i] = $data["orig_name"];

	 			//$_FILES['image'.$i]['name'];
	 			$configThumb['source_image'] = $data['full_path'];

	 			$this->image_lib->initialize($configThumb);
	 			$this->image_lib->resize();
	 			 
	 			$filename = $data['file_path'].$data['raw_name'].$date.$data['file_ext'];
	 			//$filename = $data['raw_name'].'_big'.$data['file_ext'];
	 			//echo "File name = ".$filename;
	 			rename($data['full_path'], $filename);
	 			$filename_big_arr["image".$i] = $data['raw_name'].$date.$data['file_ext'];

	 		}else
	 		//echo "<pre>"; var_dump($data); echo "</pre>";
	 		$filename = $data['file_path'].$data['raw_name'].$date.$data['file_ext'];
	 		//$filename = $data['raw_name'].'_big'.$data['file_ext'];
	 		//echo "File name = ".$filename;
	 		rename($data['full_path'], $filename);
	 		$filename_big_arr["image".$i] = $data['raw_name'].$date.$data['file_ext'];

	 		//echo "<pre>"; var_dump($filename_big_arr["image".$i]); echo "</pre>";

	 		$this->m_jobwanted->insert_file($filename_big_arr["image".$i],'usr/'.htmlspecialchars($Z_USER["user_id"]));
	 		$fileid=$this->m_jobwanted->select_max_file_id();
	 		$jobid=$this->m_jobwanted->select_max_job_id();
	 		$this->m_jobwanted->insertUserFile($jobid,$fileid);

	 	}
	 	// FOR LOOP

	 	$url_file = Zencryption::encrypt(serialize($filename_arr));
	 	$url_big_file = Zencryption::encrypt(serialize($filename_big_arr));
	 	redirect(site_url('users/profiles?type='.Zencryption::encrypt(serialize("jobs"))));
	 }

	}

	function captcha_check(){
		if (empty($_SESSION['captcha']) || trim(strtolower($_REQUEST['captcha'])) != $_SESSION['captcha'])
		{
			$request_captcha = htmlspecialchars($_REQUEST['captcha']);
			$captcha_message = "Invalid code, Please try again!";
			$request_captcha = htmlspecialchars($_REQUEST['captcha']);
			$this->form_validation->set_message('captcha_check', '<div style=\"background-color: #FF606C; width:160px; height:50px; padding:5px;\">'.$captcha_message.'</div>');
			//$data["captcha_message"]="<div style=\"background-color: #FF606C; width:160px; height:50px; padding:5px;\"><h2>$captcha_message</h2></div>";
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	//	 redirect(base_url()."index.php/job_wanted/select_upload"); test

	function select_upload(){
		$this->load->model("m_jobwanted");
		$data["selectupload"]=$this->m_jobwanted->select_upload();
		$this->load->template("templates/general","edit_upload","Job Wanted", $data);
	}

	function delete_job ($uid){
		$this->load->model("m_jobwanted");
		$this->m_jobwanted->delete_job($uid);
		$type = serialize("jobs");
		redirect(site_url("users/profiles?type=".Zencryption::encrypt($type)));
	}

	function showupdate_job($job_wan_id){
		$this->load->model("m_jobwanted");
		$data["job_id"]=$job_wan_id;
		$data["file_num"]=$this->m_jobwanted->count_file($job_wan_id);			/* function coun from model */
		$data["selectupload"]=$this->m_jobwanted->select_upload($job_wan_id);
		$data["selectimage"]=$this->m_jobwanted->select_file($job_wan_id);
		$this->load->template("templates/general","update_job","Zuka - Update job Wanted",$data);
	}
	//function selectimage($job_wan_id){
	//	   $this->load->model("m_jobwanted");
	//	   $data["selectimage"]=$this->m_jobwanted->select_file($job_wan_id);
	//	   $this->load->view("update_job",$data);
	//}

	function update_job(){
		$this->load->model("m_jobwanted");
		$jid=$this->input->post("txtjid");
		$jsub=$this->input->post("txtsub");
		$desc=$this->input->post("txtdes");
		//CheckBOX
		$chk=$this->input->post("chkchange");
		//$this->m_jobwanted->update_job($jid,$desc,$jsub);
		$this->load->library('image_lib');
		$Z_USER = Zsession::getUserFromSession();
		$this->load->helper('url_helper');
		if(isset($_POST['go'])) {
			/* Create the config for upload library */
			/* (pretty self-explanatory) */
			$date=date('His');
			$patch=FCPATH.'/usr/'.htmlspecialchars($Z_USER["user_id"]).'/'; /* NB! create this dir! */
			if (!file_exists ($patch)){ mkdir($patch,0700);}
			$config['upload_path'] = $patch;
			$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|docx|doc|rtf|pdf';
			$config['image_library'] = 'gd2';
			$config['source_image'] = $patch.htmlspecialchars($Z_USER["user_id"]);
			$config['encrypt_name']  = FALSE;
			$config['overwrite']  = TRUE;
			$config['max_size']  = '0';
			$config['max_width']  = '0';
			$config['max_height']  = '0';

			/* Load the upload library */
			$this->load->library('upload', $config);

			/* Create the config for image library */
			/* (pretty self-explanatory) */
			$configThumb = array();
			$configThumb['image_library'] = 'gd2';
			$configThumb['thumb_marker'] = $date.'_Thumb';
			$configThumb['source_image'] = '';
			$configThumb['create_thumb'] = TRUE;
			$configThumb['overwrite']  = TRUE;
			$configThumb['maintain_ratio'] = TRUE;
			/* Set the height and width or thumbs */
			/* Do not worry - CI is pretty smart in resizing */
			/* It will create the largest thumb that can fit in those dimensions */
			/* Thumbs will be saved in same upload dir but with a _thumb suffix */
			/* e.g. 'image.jpg' thumb would be called 'image_thumb.jpg' */
			$configThumb['width'] = 178;
			$configThumb['height'] = 210;
			/* Load the image library */
			$this->load->library('image_lib');

			$jsub=$_POST['txtsub'];
			$jdes=$_POST['txtdes'];
			//$this->m_jobwanted->insert_job($jsub,$jdes);
			$this->m_jobwanted->update_job($jid,$desc,$jsub);
			$jobid=$this->m_jobwanted->select_max_job_id();
		  
			$filename_arr = array();
			$filename_big_arr = array();
			for($i = 1; $i <= count($_FILES); $i++) {
				/* Handle the file upload */
				$upload = $this->upload->do_upload('image'.$i);
				/* File failed to upload - continue */
				 
				if($upload == FALSE) continue;
				/* Get the data about the file */
				$data = $this->upload->data();

				$uploadedFiles[$i] = $data;
				/* If the file is an image - create a thumbnail */
				if($data['is_image'] == 1) {
					$filename_arr["image".$i] = $data["orig_name"];

					//$_FILES['image'.$i]['name'];
					$configThumb['source_image'] = $data['full_path'];

					$this->image_lib->initialize($configThumb);
					$this->image_lib->resize();
					 
					$filename = $data['file_path'].$data['raw_name'].$date.$data['file_ext'];
					//$filename = $data['raw_name'].'_big'.$data['file_ext'];
					//echo "File name = ".$filename;
					rename($data['full_path'], $filename);
					$filename_big_arr["image".$i] = $data['raw_name'].$date.$data['file_ext'];

				}else
				//echo "<pre>"; var_dump($data); echo "</pre>";
				$filename = $data['file_path'].$data['raw_name'].$date.$data['file_ext'];
				//$filename = $data['raw_name'].'_big'.$data['file_ext'];
				//echo "File name = ".$filename;
				rename($data['full_path'], $filename);
				$filename_big_arr["image".$i] = $data['raw_name'].$date.$data['file_ext'];

				//echo "<pre>"; var_dump($filename_big_arr["image".$i]); echo "</pre>";

				$this->m_jobwanted->insert_file($filename_big_arr["image".$i],'usr/'.htmlspecialchars($Z_USER["user_id"]));
				$fileid=$this->m_jobwanted->select_max_file_id();
				//$jobid=$this->m_jobwanted->select_max_job_id();
				$this->m_jobwanted->insertUserFile($jid,$fileid);

			}
			// FOR LOOP
		}
		 
		redirect(site_url('users/profiles?type='.Zencryption::encrypt(serialize("jobs"))));
	}
	/* Select File and Descrition To show */
	//function showImage($job_wan_id){
	//       $this->load->model("m_jobwanted");
	//       $data["selectimage"]=$this->m_jobwanted->select_file($job_wan_id);
	//       $this->load->view("job_wanted/want_job",$data);
	//}
	function detail($job_wan_id){
		$this->load->model("m_jobwanted");
		$data["select"]=$this->m_jobwanted->get_all_job_wanted($job_wan_id);
		$data["selectimage"]=$this->m_jobwanted->select_file($job_wan_id);
		
		$this->m_jobwanted->insert_jobwanted_hit($job_wan_id);
		$this->load->template("templates/general","job_wanted/want_job","Job Wanted",$data);
	}
	function select_f_donw($file_id){
		$this->load->model("m_jobwanted");
		$data["selectimage"]=$this->m_jobwanted->select_file($file_id);

	}
	/* End File and Descrition To show */
	function delete_file($job_wan_id,$file_id){
		$this->load->model("m_jobwanted");
		$this->m_jobwanted->delete_f_edit($file_id);
		$this->m_jobwanted->delete_file($file_id);
		redirect(site_url("job_wanted/showupdate_job/".$job_wan_id));
		 
		 
		 
	}
}
?>
